package wxk.interview.serializable;

import java.io.*;

/**
 *
 * 父类序列化问题
 *
 * 若父类没有实现serializable接口，则父类的字段不能被序列化，子类反序列化取到的值为默认值
 * @author wangxuekai
 * @version 2.2
 * @Date 2017/3/9.
 * @since 2.2
 */
public class PersonLife extends Life implements Serializable {
    private static final long serialVersionUID = 7030715539422327866L;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "PersonLife{" +
                "name='" + name + '\'' +
                '}';
    }

    public static void main(String[] args){
        PersonLife pl = new PersonLife();
        pl.setName("123");
        pl.setType("人类");

        File file = new File("personLife.out");

        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
            outputStream.writeObject(pl);

            ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
            PersonLife npl = (PersonLife) inputStream.readObject();

            System.out.println(pl);
            System.out.println(npl);
            System.out.println(npl.getType());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
